package ms.unicorn.simulation;

import java.util.ArrayList;
import java.util.List;

public class FDNode {
	public static final String[] NODE_TYPE = new String[] {"REQUIREMENT", "COMPOUND"};
	
	public static final int FDNODE_REQUIREMENT = 0;
	public static final int FDNODE_COMPOUND = 1;
	
	private DAT dat;
	private String name;
	private final int nodetype;
	private final List<FDNode> childnodes;
	private final List<FDNode> pnodes;
	
	public FDNode(int nodetype) {
		childnodes = new ArrayList<FDNode>();
		pnodes = new ArrayList<FDNode>();
		this.nodetype = nodetype;
	}
	
	public FDNode(int nodetype, String name) {
		this(nodetype);
		this.name = name;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(500);
		toString(builder, 0);
		return builder.toString();
	}
	
	public void toString(StringBuilder builder, int indent) {
		// indent the string
		for (int i = 0; i<indent; i++, builder.append('\t'));
		// append node name
		builder.append(String.format("%s %s: ", NODE_TYPE[nodetype], name));
		// append dat if applicable
		if (dat != null)
			builder.append(dat.toString());
		// append children, if any?
		if (childnodes.size() > 0) {
			for (FDNode child: childnodes) {
				builder.append('\n');
				child.toString(builder, indent + 1);
			}
		}
	}

	public List<FDNode> getParentNode() {
		return pnodes;
	}

	public void addParentNode(FDNode parentNode) {
		if (!pnodes.contains(parentNode)) {
			pnodes.add(parentNode);
			if (!parentNode.getChildNode().contains(this))
				parentNode.getChildNode().add(this);
		}
	}
	
	public List<FDNode> getChildNode() {
		return childnodes;
	}

	public int getNodetype() {
		return nodetype;
	}

	
	public DAT getDAT() {
		return dat;
	}

	
	public void setDAT(DAT dat) {
		this.dat = dat;
	}

	public boolean isLeaf() {
		return childnodes.size() == 0;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
